<?php
ini_set('display_errors', 1);
error_reporting(-1);
ini_set('memory_limit', '512M');
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\brandAccount\BrandAccountInfo;
use Biz\model\brandAccount\BrandAccountShop;

$shopUid = BrandAccountInfo::getAllByWhere(
    ['account_status' => BrandAccountInfo::ACCOUNT_STATUS_USED],
    ['id', 'uid']);
$shopUidMap = array_column($shopUid, 'uid', 'id');


$shop = BrandAccountShop::getAllByWhere([
    'account_id' => array_keys($shopUidMap),
    'shop_id[>]' => 0,
    'shop_status' => BrandAccountShop::SHOP_STATUS_VALID
], ['type', 'shop_id', 'account_id']);
$map = [];
foreach ($shop as $v) {
    $map[$v['type']][$v['shop_id']] = $shopUidMap[$v['account_id']];
}


$list = \Biz\model\wiki\Wiki::select(['id', 'channel_type', 'channel_shop_id', 'shop_uid'], ['status[!]' => 9]);


$error = [];
foreach ($list as $v) {
    $channel_type = $v['channel_type'];
    $channel_shop_id = $v['channel_shop_id'];
    $shop_uid = $v['shop_uid'];
    $id = $v['id'];
    if (in_array($v['channel_type'], [1, 2])) {
        $check_shop_uid = $map[1][$channel_shop_id] ?? 0;
        $check_shop_uid = $check_shop_uid > 0 ? $check_shop_uid : $map[2][$channel_shop_id] ?? 0;
    } else {
        $check_shop_uid = $map[$channel_type][$channel_shop_id] ?? 0;
    }
    if ($check_shop_uid != $shop_uid) {
        echo "id $id|channel_type $channel_type|channel_shop_id $channel_shop_id|shop_uid $shop_uid|check $check_shop_uid", PHP_EOL;
//        \Biz\model\wiki\Wiki::update(['shop_uid' => $check_shop_uid], ['id' => $id]);
//        \Doraemon\tools\Log::info('修复数据', compact('id', 'channel_type', 'channel_shop_id', 'shop_uid', 'check_shop_uid'));
    }
}

////todo 查询待处理的wiki
//$list = \Biz\model\wiki\Wiki::select(['id','channel_type', 'channel_shop_id', 'shop_uid'], ['status[!]' => 9]);
//foreach ($list as $v) {
//    $v['channel_spu_id'];
//    $nick = $rs['nick'] ?? '';
//    if ($nick === '	青岛木格家具厂') {
//        var_dump($v['id']);
//        \Biz\model\wiki\Wiki::update(['channel_shop_name' => $nick, 'channel_shop_id' => 60017232], ['id' => $v['id']]);
//    }
//}
